package com.ymatou.app.rec.testcase;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.json.JSONArray;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.app.rec.parameter.C2cBean;
import com.ymatou.app.rec.service.C2cCall;
import com.ymatou.app.rec.service.c2cLiveIndexBizerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.database.sqlwapper.AppDefaultWapper;
/**
*视频直播搜索接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_C2c{
	 private static C2cBean c2clivebizerBean;
	 private static C2cCall c2clivebizerCall;
	 private static c2cLiveIndexBizerCall c2cLiveIndexBizerCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播搜索接口");
	}

	@Before
	public void caseUp() {
		c2clivebizerBean=new C2cBean();
		c2clivebizerCall=new C2cCall();
		c2cLiveIndexBizerCall = new c2cLiveIndexBizerCall();
		c2clivebizerBean.setAppkey(3);
		c2clivebizerBean.setSort("desc(yscore)");
		c2clivebizerBean.setModule("live_list");
		c2clivebizerBean.setPlatform("pc");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(c2clivebizerCall.getOpurl());
		//Logger.generateResult();
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_C2c_001() {
		Logger.start(true,"按关键字搜索接口返回是否有异常");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			
			c2clivebizerBean.setFilter("");
			c2clivebizerBean.setFl("id,isReplay");
			c2clivebizerBean.setQuery("keyword(keyword,百货)");
			
			c2clivebizerCall.setData(c2clivebizerBean);
			c2clivebizerCall.callService();			
			Logger.comment("c2clivebizerCall send:" + c2clivebizerCall.getEntityString());
			Logger.comment("c2clivebizerCall response:" + c2clivebizerCall.getReturnData());
			Logger.verifyEquals("OK",c2clivebizerCall.getStatus(), "按关键字搜索调用成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_C2c_002() {
		Logger.start(true,"按大洲搜索接口调用状态");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			c2clivebizerBean.setFilter("");
			c2clivebizerBean.setFl("countryGroupName,sellerId,isReplay,iSort");
			// 1:北美，2：欧洲，3：亚洲，4：澳新
			c2clivebizerBean.setQuery("term(countryGroupId,3)");//countryGroupId表示大州所代表的id值
			c2clivebizerCall.setData(c2clivebizerBean);
			c2clivebizerCall.callService();			
			Logger.comment("c2clivebizerCall send:" + c2clivebizerCall.getEntityString());
			Logger.comment("c2clivebizerCall response:" + c2clivebizerCall.getReturnData());
			Logger.verifyEquals("OK",c2clivebizerCall.getStatus(), "按大州搜索调用成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_C2c_003() {
		Logger.start(true,"增加时间过滤条件查看返回状态");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			c2clivebizerBean.setFilter("and(range(beginTime,null,2016-01-25 01:36:00,true,true),range(endTime,2016-01-27 23:00:00,null,true,true))");
			c2clivebizerBean.setFl("id,isReplay");
			c2clivebizerBean.setQuery("keyword(keyword,日本)");
			c2clivebizerCall.setData(c2clivebizerBean);
			c2clivebizerCall.callService();			
			Logger.comment("c2clivebizerCall send:" + c2clivebizerCall.getEntityString());
			Logger.comment("c2clivebizerCall response:" + c2clivebizerCall.getReturnData());
			Logger.verifyEquals("OK",c2clivebizerCall.getStatus(), "增加时间过滤调用成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_C2c_004() {
		Logger.start(true,"按关键字搜索返回记录数");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			c2clivebizerBean.setFilter("");
			c2clivebizerBean.setFl("id,isReplay");
			c2clivebizerBean.setQuery(null);
			c2clivebizerCall.setData(c2clivebizerBean);
			c2clivebizerCall.callService();		
			
			//访问索引文件
			c2cLiveIndexBizerCall.setUrl("http://es.ymatou.com:19200/c2clive/_search");
			c2cLiveIndexBizerCall.callService();	
			
			//获取索引文件中的总记录数
			Integer indexTotal = (Integer) c2cLiveIndexBizerCall.getData().get("total");
			
			Logger.comment("c2clivebizerCall send:" + c2clivebizerCall.getEntityString());
			Logger.comment("c2clivebizerCall response:" + c2clivebizerCall.getReturnData());
			//查询出的数据还需要过滤掉未认证的买手
			Logger.verifyEquals(indexTotal,c2clivebizerCall.getTotal(), "接口返回记录数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_C2c_005() {
		Logger.start(true,"直播区域统计接口返回状态");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			c2clivebizerBean.setPlatform("appbuyer");
			c2clivebizerBean.setModule("mainhome");
			c2clivebizerBean.setStat("term(countryGroupId)");
			c2clivebizerBean.setLimit("0,10");
			c2clivebizerBean.setInfo("timenow:20160127143200,cookieid:6500D309-609D-4CAE-8A35-50A7972361C5,userid:3592231");
			c2clivebizerCall.setData(c2clivebizerBean);
			c2clivebizerCall.callService();			
			Logger.comment("c2clivebizerCall send:" + c2clivebizerCall.getEntityString());
			Logger.comment("c2clivebizerCall response:" + c2clivebizerCall.getReturnData());
			Logger.verifyEquals("OK",c2clivebizerCall.getStatus(), "直播区域统计接口返回状态ok");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_C2c_006() {
		Logger.start(true,"直播区域统计接口比较时间及状态验证返回id值有效性验证 ");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			c2clivebizerBean.setPlatform("appbuyer");
			c2clivebizerBean.setModule("mainhome");
			c2clivebizerBean.setStat("term(countryGroupId)");
			c2clivebizerBean.setLimit("0,10");
			c2clivebizerBean.setInfo("timenow:20160329143200,cookieid:6500D309-609D-4CAE-8A35-50A7972361C5,userid:3592231");
			c2clivebizerCall.setData(c2clivebizerBean);
			c2clivebizerCall.callService();			
			Logger.comment("c2clivebizerCall send:" + c2clivebizerCall.getEntityString());
			Logger.comment("c2clivebizerCall response:" + c2clivebizerCall.getReturnData());
			//得到hits里的所有id，此id为iactivityid
			JSONArray Hits = c2clivebizerCall.getHits();
			//取第一条查看是否有效
			String hits=Hits.get(0).toString();
			JSONObject json=JSONObject.fromObject(hits);
			String id = json.getString("id");
			//查找Ymt_Activity表中的这条记录
			List<Map> activityTime = new AppDefaultWapper().getActivityTime(id);
			//比较当前时间是否比小于结束时间
			Date time= (Date) activityTime.get(0).get("dEndTime");
			long nowTime = new Date().getTime();
			long endTime = time.getTime();
			
			//比较两个时间点
			Logger.verifyEquals(true, nowTime<endTime, "已过期的直播也存在了！");
			
			//取状态值
			int iaction =(Integer) activityTime.get(0).get("iAction");
			//比较状态值
			Logger.verifyEquals(1,iaction, "状态不为1的直播也存在了！");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_C2c_007() {
		Logger.start(true,"直播区域统计接口返回值是否存在重复性 ");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			c2clivebizerBean.setPlatform("appbuyer");
			c2clivebizerBean.setModule("mainhome");
			c2clivebizerBean.setStat("term(countryGroupId)");
			c2clivebizerBean.setLimit("0,100");
			c2clivebizerBean.setInfo("timenow:20160129143200,cookieid:6500D309-609D-4CAE-8A35-50A7972361C5,userid:3392231");
			c2clivebizerCall.setData(c2clivebizerBean);
			c2clivebizerCall.callService();			
			Logger.comment("c2clivebizerCall send:" + c2clivebizerCall.getEntityString());
			Logger.comment("c2clivebizerCall response:" + c2clivebizerCall.getReturnData());
			//得到hits里的所有id，此id为iactivityid
			JSONArray hits = c2clivebizerCall.getHits();
			List<Integer> list = new java.util.ArrayList<Integer>();
			
			for(int i=0;i<hits.length();i++){
				
			String ids	= hits.get(i).toString();
			JSONObject json=JSONObject.fromObject(ids);
			int id = json.getInt("id");
			if(!list.contains(id)){//判断是否包含存在的值，包含返回true
				list.add(id);
			}
			
			}
			Logger.verifyEquals(list.size(),hits.length(), "是否存在重复值");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_C2c_008() {
		Logger.start(true,"直播区域统计接口是否返回groupid ");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			c2clivebizerBean.setPlatform("appbuyer");
			c2clivebizerBean.setModule("mainhome");
			c2clivebizerBean.setStat("term(countryGroupId)");
			c2clivebizerBean.setLimit("0,100");
			c2clivebizerBean.setInfo("timenow:20160129143200,cookieid:6500D309-609D-4CAE-8A35-50A7972361C5,userid:3392231");
			c2clivebizerCall.setData(c2clivebizerBean);
			c2clivebizerCall.callService();			
			Logger.comment("c2clivebizerCall send:" + c2clivebizerCall.getEntityString());
			Logger.comment("c2clivebizerCall response:" + c2clivebizerCall.getReturnData());
			boolean statsRestul = c2clivebizerCall.statsRestul();
			Logger.verifyEquals(true, statsRestul, "返回接口是否包含stat的key值");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_C2c_009() {
		Logger.start(true,"直播区域统计接口返回统计值是否正确 ");
		try {
			//TODO 补全case代码
			//org.junit.Assert.fail();
			c2clivebizerBean.setPlatform("appbuyer");
			c2clivebizerBean.setModule("mainhome");
			c2clivebizerBean.setStat("term(countryGroupId)");
			c2clivebizerBean.setLimit("0,10");
			c2clivebizerBean.setInfo("timenow:20160129143200,cookieid:6500D309-609D-4CAE-8A35-50A7972361C5,userid:3392231");
			c2clivebizerCall.setData(c2clivebizerBean);
			c2clivebizerCall.callService();			
			Logger.comment("c2clivebizerCall send:" + c2clivebizerCall.getEntityString());
			Logger.comment("c2clivebizerCall response:" + c2clivebizerCall.getReturnData());
			//得到接口返回total值
			
			 int total = c2clivebizerCall.getTotal();
			//得到hits里的所有id，此id为iactivityid
			Object groupAry = c2clivebizerCall.getStats();
			JSONObject json=JSONObject.fromObject(groupAry);
			JSONObject activityNumAry= (JSONObject) json.get("countryGroupId");
			int num = 0;
			//用Iterator迭代器遍历取值
			Iterator iterator = activityNumAry.keys();
			while(iterator.hasNext()){
			     String	key = (String) iterator.next();
			     String value = activityNumAry.getString(key);
			     int i =Integer.parseInt(value);
			     num = i+num;
			     
			}
			
			Logger.verifyEquals(total,num, "州统计值相加与总数是否相等");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}